﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK.IService.WCS;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Wcs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.WCS
{
    public class WCSService :BaseService, IWCSService
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IPutawayOrderRepository putawayOrderRepository;
        private readonly IPickingOrderRepository pickingOrderRepository;
        private readonly IStationRepository stationRepository;
        private readonly IReceivingOrderDetailService receivingOrderDetailService;
        private readonly IShippingOrderDetailService shippingOrderDetailService;
        private readonly ITransferBinOrderService transferBinOrderService;
        private readonly WcsHttpClient wcsHttpClient;

        public WCSService(IMapper mapper, IUnitOfWork unitOfWork, IPutawayOrderRepository putawayOrderRepository,IPickingOrderRepository pickingOrderRepository, IStationRepository stationRepository, IReceivingOrderDetailService receivingOrderDetailService, IShippingOrderDetailService shippingOrderDetailService, ITransferBinOrderService transferBinOrderService, WcsHttpClient wcsHttpClient):base(mapper)
        {
            this.unitOfWork = unitOfWork;
            this.putawayOrderRepository = putawayOrderRepository;
            this.pickingOrderRepository = pickingOrderRepository;
            this.stationRepository = stationRepository;
            this.receivingOrderDetailService = receivingOrderDetailService;
            this.shippingOrderDetailService = shippingOrderDetailService;
            this.transferBinOrderService = transferBinOrderService;
            this.wcsHttpClient = wcsHttpClient;
        }

        /// <summary>
        /// 任务取消接口
        /// </summary>
        /// <param name="palletCode"></param>
        /// <returns></returns>
        public bool CancelTaskApi(string palletCode, out string message)
        {
            //只能取消出库任务，因为入库任务创建完就肯定已在执行当中 因为无法取消已执行的任务
            message = string.Empty;
            bool result = false;
           
            result = shippingOrderDetailService.WCSCancelTask(palletCode, out message);
            if (!result)
            {
                return false;
            }
            message = "取消完成!";
            return result;
        }

        /// <summary>
        /// 任务入库申请
        /// </summary>
        /// <param name="taskInRequest"></param>
        /// <returns></returns>
        public bool GetTaskByPallet(TaskInRequest taskInRequest,out string Msg)
        {
            Msg = string.Empty;
            var pickingOrderObj = pickingOrderRepository.QueryEntityWhere(s => s.Status == 4 && s.SourceType == 2);
            if (pickingOrderObj != null)
            {
                Msg = "当前有出库任务！";
                return false;
            }
            //查看站台模式，必须为入库才可以 A01为固定值
            var station = stationRepository.QueryEntityWhere(s=>s.Code == "A01");
            if (station is null)
            {
                Msg = "未查询到入库口信息！";
                return false;
            }
            if (station.FunctionType != 1)
            {
                Msg = "当前模式不是入库!";
                return false;
            }
            //获取托盘入库信息
            var putaway = putawayOrderRepository.QueryEntityWhere(p => p.Status == 4 && p.PalletBarcode == taskInRequest.BarCode && p.SourceType == 1);
            if (putaway is null)
            {
                Msg = "托盘没有入库信息！";
                return false;
            }
            unitOfWork.BeginTransaction();
            //修改上架单站台数据
            putaway.SiteCode = station.Code;
            putawayOrderRepository.UpdEntity(putaway);
            if (!unitOfWork.SaveChangesEntity())
            {
                unitOfWork.RollbackChanges();
                Msg = "修改上架单站点失败！";
                return false;
            }
            //创建任务 
            List<Tasks> tasks = new List<Tasks>
            {
                new Tasks()
                {
                    BarCode = putaway.PalletBarcode,
                    TaskId = putaway.Code,
                    TaskType = 0,
                    EndNode = putaway.LocationCode,
                    StartNode = putaway.SiteCode,
                    Order = 1

                }
            };
            TaskAccept taskAccept = new TaskAccept();
            taskAccept.GroupId = Guid.NewGuid().ToString("N");
            taskAccept.MsgTime = DateTime.Now.ToString();
            taskAccept.PriorityCode = 0;
            taskAccept.Warehouse = Config.WMSWarehouseCode; //库房编号
            taskAccept.tasks = tasks;
            if (Config.IsDev == false)
            {
                var wcsApiRes = wcsHttpClient.TaskAcceptApi(taskAccept);//下发入库任务
                if (wcsApiRes.returnStatus == 1)
                {
                    unitOfWork.RollbackChanges();
                    Msg = wcsApiRes.returnInfo;
                    return false;
                }
            }
            unitOfWork.CommitChanges();
            Msg = "申请成功！";
            return true;
        }

        /// <summary>
        /// 任务状态上报 
        /// </summary>
        /// <param name="reportTaskData"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool ReportTask(ReportTaskData reportTaskData, out string Msg)
        {
            Msg = string.Empty;
            bool result = false;
            //获取返回任务类型并完成任务
            string end = reportTaskData.TaskId.Substring(0, 3);
            if (end == "PUT" && reportTaskData.TaskStatus == 8)
                result = receivingOrderDetailService.FinishReceivingOrderDetail(reportTaskData.TaskId, out Msg);
            else if (end == "OUT" && reportTaskData.TaskStatus == 8)
                result = shippingOrderDetailService.FinishShippingOrderDetail(reportTaskData.TaskId, out Msg);
            else if (end == "ORT" && reportTaskData.TaskStatus == 8)
                result = shippingOrderDetailService.FinishShippingOutZeroQty(reportTaskData.TaskId, out Msg);
            else if (end == "SRT" && reportTaskData.TaskStatus == 8)
                result = shippingOrderDetailService.FinishPickingShippingOrderDetail(reportTaskData.TaskId, out Msg);
            else if (end == "TRA" && reportTaskData.TaskStatus == 8)
                result = transferBinOrderService.FinishTransferBinOrder(reportTaskData.TaskId, out Msg);

            if (result)
            {
                Msg = "完成任务!";
                return true;
            }
            Msg = "任务失败，" + Msg; 
            return false;
        }
    }
}
